﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Tunnel.Tools
{
    static class FanTimeControl
    {

        public static void init()
        {
            try
            {
                SqlDBHelper SDH = new SqlDBHelper();
                string sqlSearchCommandTime = "SELECT LastFanTime FROM M_LastFanTime";
                DataTable dtRunStatus = SDH.GetTable(sqlSearchCommandTime);

                foreach (DataRow dr in dtRunStatus.Rows)
                {
                    LastFanTime = Convert.ToDateTime(dr["LastFanTime"].ToString());
                }
            }
            catch
            {
                LastFanTime = DateTime.Now;
            }
        }
        public static int getSeconds()
        {            
            TimeSpan ts = new TimeSpan();
            DateTime dt1 = DateTime.Now;
            ts = dt1 - LastFanTime;
            double getSeconds = ts.TotalSeconds;

            int fanIntervalTime = Properties.Settings.Default.FanIntervalTime;

            int seconds = fanIntervalTime - (int)getSeconds;
            if (seconds <= 0)
            {
                return 0;
            }
            else
            {
                return seconds;
            }
        }

        public static void SaveLastTime()
        {
            string sql = "UPDATE M_LastFanTime SET LastFanTime='" + LastFanTime.ToString() + "'";
        }

        private static DateTime lastFanTime;
        public static DateTime LastFanTime
        {
            get
            {
                return lastFanTime;
            }

            set
            {
                lastFanTime = value;
            }
        }
    }
}
